﻿//***********************************************************************
//* Module:  TbBstPoliceDAL
// * 作者:  曹荣芝
//* Modified: 2013-04-01
// * Purpose: Definition of the Class TbBstPoliceDAL
// ***********************************************************************/
using System;
using System.Data;
using XWG.Framework.AuthEntity;
using XWG.JWBST.JWBSTEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTSPConst;
using XWG;
using XWG.Framework.MSException;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data.OracleClient;
namespace XWG.JWBST.JWBSTDAL
{
    /// <summary>
    /// 
    /// </summary>
    public class TbBstPoliceDAL
    {
        #region 删除操作
        /// <summary>
        /// 删除。
        /// </summary>
        /// <param name="policepkid">要删除的的主键值。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>是否成功 1 表示成功 负值代表失败</returns>
        public static ResultModel Delete(object policepkid, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();//定义错误信息类
            try
            {
                //载入实例
                JWBSTEntity.TbBstPolice u = JWBSTDAL.TbBstPoliceDAL.Load(policepkid) as JWBSTEntity.TbBstPolice;

                if (isLog)//如果写入日志
                {
                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "删除";
                    log.LogObject = TbBstPoliceConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    //Db.TransDelete<TbBstPolice, AppLog>(u, log);
                    u.PoliceDel = "0";
                    Db.TransUpdate<TbBstPolice, AppLog>(u, log);
                }
                else//不写入日志
                {
                    Db.TransDelete(u);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(policepkid);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9994;
                resultModel.FiledKey = Convert.ToString(policepkid);
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的删除操作");
                return resultModel;

            }
        }
        #endregion

        #region 根据主键查询一条记录
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="policepkid">主键</param>
        /// <returns>用户实体</returns>
        public static JWBSTEntity.TbBstPolice Load(object policepkid)
        {
            return Db.GetModel<JWBSTEntity.TbBstPolice>(policepkid);
        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 查询
        /// </summary>
        public static IList<TbBstPolice> SelectList()
        {
            try
            {
                ICriteria crt = Db.Session.CreateCriteria(typeof(TbBstPolice));
                //crt.Add(NHibernate.Criterion.Expression.Eq(TbBstPoliceConst.Columns.EnabledPropertyName, Enabled.TRUE));
                IList<TbBstPolice> list = crt.List<TbBstPolice>();
                return list;
            }
            catch (Exception ex)
            {
                XWG.Framework.MSException.MSEx.DealErr(ex, "查询");
                return null;
            }
            finally
            {

            }
        }
        #endregion
        #region 插入操作
        /// <summary>
        /// 新增。
        /// </summary>
        /// <param name="info">待新增的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>新增是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Insert(JWBSTEntity.TbBstPolice info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            object filedkey;
            try
            {
                if (isLog)
                {

                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "新建";
                    log.LogObject = TbBstPoliceConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    filedkey = Db.TransInsert<TbBstPolice, AppLog>(info, log);
                }
                else
                {
                    filedkey = Db.TransInsert<TbBstPolice>(info);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(filedkey);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9998;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的插入操作");
                return resultModel;
            }
        }
        #endregion

        #region 修改操作
        /// <summary>
        /// 修改。
        /// </summary>
        /// <param name="info">待修改的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>修改是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Update(JWBSTEntity.TbBstPolice info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                if (isLog)
                {


                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "修改";
                    log.LogObject = TbBstPoliceConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    Db.TransUpdate<TbBstPolice, AppLog>(info, log);
                }
                else
                {
                    Db.TransUpdate<TbBstPolice>(info);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {
                resultModel.ErrorCode = -9997;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的修改操作");
                return resultModel;
            }
        }
        #endregion

        #region 根据坐标查找最近5个警点
        /// <summary>
        /// 查找最近5个警点
        /// </summary>
        /// <param name="police_type">要查询警点的类型</param>
        /// <param name="GPS_X">要查询警点的X坐标</param>
        /// <param name="GPS_Y">要查询警点的Y坐标</param>
        /// <returns></returns>
        public static List<TBstFindPolice> SearchPolice(string police_type, string GPS_X, string GPS_Y)
        {
            try
            {
                List<TBstFindPolice> spolice = new List<TBstFindPolice>();
                StringBuilder sql = new StringBuilder();
                sql.Append("select *　from (");
                sql.Append("select police_pkid,police_name,police_address,police_gps_x_bd,police_gps_y_bd,(abs(:GPS_X - police_gps_x_bd)) + (abs(:GPS_Y - police_gps_y_bd)) as gps from tb_bst_police where police_type = :police_type order by gps)");
                sql.Append("where rownum <= 5");
                //string sql = "select *　from (select police_pkid,police_name,police_address,police_gps_x_bd,police_gps_y_bd,(abs(:GPS_X - police_gps_x_bd)) + (abs(:GPS_Y - police_gps_y_bd)) as gps from tb_bst_police where police_type = :police_type order by gps) where rownum <= 5";

                OracleParameter[] sp = { 
                                  new OracleParameter(":police_type",police_type),
                                   new OracleParameter(":GPS_X",GPS_X),
                                   new OracleParameter(":GPS_Y",GPS_Y)
                                   };

                DataTable mes = OrclHelper.GetDT(sql.ToString(), sp);
                if (mes != null && mes.Rows.Count > 0)
                {
                    for (int i = 0; i < mes.Rows.Count; i++)
                    {
                        TBstFindPolice police = new TBstFindPolice();
                        police.PolicePkid = mes.Rows[i]["police_pkid"].ToString();
                        police.PoliceName = mes.Rows[i]["police_name"].ToString();
                        police.PoliceAddress = mes.Rows[i]["police_address"].ToString();
                        police.PoliceGpsX = mes.Rows[i]["police_gps_x_bd"].ToString();
                        police.PoliceGpsY = mes.Rows[i]["police_gps_y_bd"].ToString();
                        spolice.Add(police);
                    }
                }
                return spolice;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion

        #region 根据policetype查询相对应的数据
        /// <summary>
        /// 根据policetype查询相对应的数据
        /// </summary>
        /// <param name="policetype">要查询警点的类型</param>
        /// <returns></returns>
        public static List<TBstFindPolice> FindPolice(string keyword)
        {
            try
            {
                List<TBstFindPolice> spolice = new List<TBstFindPolice>();
                string sql = "select police_pkid,police_name,police_address,police_gps_x_bd,police_gps_y_bd from tb_bst_police where police_name like '%"+keyword+"%' and police_del = '0' order by police_code";
                //OracleParameter[] sp = { 
                //                   new OracleParameter(":police_name",keyword)
                //                   };
                DataTable mes = OrclHelper.GetDT(sql);
                if (mes != null && mes.Rows.Count > 0)
                {
                    for (int i = 0; i < mes.Rows.Count; i++)
                    {
                        TBstFindPolice police = new TBstFindPolice();
                        police.PolicePkid = mes.Rows[i]["police_pkid"].ToString();
                        police.PoliceName = mes.Rows[i]["police_name"].ToString();
                        police.PoliceAddress = mes.Rows[i]["police_address"].ToString();
                        police.PoliceGpsX = mes.Rows[i]["police_gps_x_bd"].ToString();
                        police.PoliceGpsY = mes.Rows[i]["police_gps_y_bd"].ToString();
                        spolice.Add(police);
                    }
                }
                return spolice;
            }
            catch (Exception ex)
            {

                throw;
            }

        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 查询-根据code
        /// </summary>
        public static IList<TbBstDic> GetDictionaryByCode(string Parentnode)
        {
            try
            {
                ICriteria crt = Db.Session.CreateCriteria(typeof(TbBstDic));
                crt.Add(NHibernate.Criterion.Expression.Eq(TbBstDicConst.Columns.ParentnodePropertyName, Parentnode));
                IList<TbBstDic> list = crt.List<TbBstDic>();
                return list;
            }
            catch (Exception ex)
            {
                XWG.Framework.MSException.MSEx.DealErr(ex, "查询");
                return null;
            }
            finally
            {

            }
        }


        /// <summary>
        /// 查询-根据code和Parentnode
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public static IList<TbBstDic> GetDicByCodeAndParentnode(string Code, string Parentnode)
        {
            try
            {
                ICriteria crt = Db.Session.CreateCriteria(typeof(TbBstDic));
                crt.Add(NHibernate.Criterion.Expression.Eq(TbBstDicConst.Columns.ParentnodePropertyName, Parentnode));
                crt.Add(NHibernate.Criterion.Expression.Eq(TbBstDicConst.Columns.CodePropertyName, Code));
                IList<TbBstDic> list = crt.List<TbBstDic>();
                return list;
            }
            catch (Exception ex)
            {
                XWG.Framework.MSException.MSEx.DealErr(ex, "查询");
                return null;
            }
            finally
            {

            }

        #endregion

        }
    }
}
